package com.example.lwj.zhangtianchengnews;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.example.lwj.FragmentDemo.MainFragment;
import com.example.lwj.FragmentDemo.MenuFragment;
import com.example.lwj.utils.CommonUtls;
import com.example.lwj.utils.TitileUtils;
import com.example.lwj.view.viewPagerindicator;
import com.google.android.gms.common.api.GoogleApiClient;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {

    private ViewPager vp;
    private viewPagerindicator vpager;
    private SlidingMenu slidingMenu;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String newstitle = CommonUtls.getSp("newsTitle");
        String[] split = newstitle.split(",");

        for (int i = 0; i < 20; i++) {
            TitileUtils.myChannel.add(split[i]);
        }
        for (int i=20;i<split.length;i++){
            TitileUtils.moreChannel.add(split[i]);
        }
        //找到控件
        vp = (ViewPager) findViewById(R.id.vp);
        vpager = (viewPagerindicator) findViewById(R.id.viewPager);
        //添加适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return MainFragment.getData(  TitileUtils.myChannel.get(position).substring(  TitileUtils.myChannel.get(position).indexOf(":")+1));
            }

            @Override
            public int getCount() {
                return   TitileUtils.myChannel.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return   TitileUtils.myChannel.get(position).substring(0,   TitileUtils.myChannel.get(position).indexOf(":"));
            }
        });


        // 设置侧拉条目布局
        setBehindContentView(R.layout.menu);
        // 获取侧拉栏目对象
        slidingMenu = getSlidingMenu();

        /*
         * SlidingMenu.TOUCHMODE_FULLSCREEN全屏触摸有效 SlidingMenu.TOUCHMODE_MARGIN
         * 拖拽边缘有效 SlidingMenu.TOUCHMODE_NONE 不响应触摸事件
         */
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        // 设置内容显示页对应的dp大小
        slidingMenu.setBehindOffsetRes(R.dimen.activity_vertical_margin);
        // //设置左侧侧拉栏目宽度
        // slidingMenu.setBehindWidth(140);
        // 设置侧拉栏目所在位置
        /*
         * SlidingMenu.LEFT SlidingMenu.LEFT_RIGHT SlidingMenu.RIGHT
         */
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 给侧拉栏目和左侧内容页区分开(加线)
        slidingMenu.setShadowDrawable(R.mipmap.ic_launcher);
        // 设置线的宽度
        slidingMenu.setShadowWidthRes(R.dimen.activity_horizontal_margin);

        // fragment去替换布局中节点
        MenuFragment menuFragment = new MenuFragment();
        // FragmentManager管理者
        getSupportFragmentManager()
                // 开启事物
                .beginTransaction()
                // 通过fragment去替换对应布局
                .replace(R.id.menu, menuFragment, "MENU")
                // 提交事物
                .commit();
        //与viewPager关联
        vpager.setViewpager(vp, slidingMenu);

    }

}
